package com.vinoxm.rss.dao;

import com.vinoxm.common.core.dao.BaseDao;
import com.vinoxm.rss.model.RssTracker;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Select;

import java.util.List;

public interface RssTrackerDao extends BaseDao {

    @Insert("INSERT INTO rss_tracker(host) VALUE(#{host})")
    void insertOneTracker(RssTracker tracker);

    @Insert("<script>" +
            "INSERT IGNORE INTO rss_tracker(host) VALUES " +
            "<foreach collection='list' item='item' separator=','>" +
            "(#{item.host})" +
            "</foreach>" +
            "</script>")
    void insertManyTracker(List<RssTracker> list);

    @Insert("<script>" +
            "INSERT IGNORE INTO rss_tracker(id, host) VALUES " +
            "<foreach collection='list' item='item' separator=','>" +
            "(#{item.id}, #{item.host})" +
            "</foreach>" +
            "</script>")
    int insertManyTrackerWithId(List<RssTracker> list);

    @Select("SELECT COALESCE(id, -1) FROM rss_tracker WHERE host=#{host}")
    Long selOneTrackerId(String host);

    @Select("<script>" +
            "SELECT COALESCE(GROUP_CONCAT(id), '') tracker FROM rss_tracker WHERE host IN(" +
            "<foreach collection='list' item='item' separator=','>" +
            "#{item}" +
            "</foreach>" +
            ")" +
            "</script>")
    String selManyTrackerIdWithConcat(List<String> hosts);

    @Select("SELECT COALESCE(MAX(id),0) id FROM rss_tracker")
    Long selMaxTrackerId();

    @Select("SELECT id,host FROM rss_tracker")
    List<RssTracker> selAll();
}
